package com.hskn.hss.module.totalcost.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlcostoperate.entity.TlCostOperate;
import com.hskn.hss.module.tlempincidental.entity.TlEmpIncidental;
import com.hskn.hss.module.tlempsalary.entity.TlEmpSalary;
import com.hskn.hss.module.totalcost.dto.SummaryVehicleCostDTO;
import com.hskn.hss.module.totalcost.service.TotalCostService;
import com.hskn.hss.module.totalcost.to.SummaryOperatingExpensesTO;
import com.hskn.hss.module.totalcost.to.SummaryPersonnelCostsTO;
import com.hskn.hss.module.totalcost.to.SummaryTotalCostTO;
import com.hskn.hss.module.totalcost.to.SummaryVehicleCostTO;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 成本汇总
 * </p>
 *
 * @author hskn
 * @since 2021-05-15
 */
@Slf4j
@RestController
@RequestMapping("/hss/totalcost")
@Api(tags = "-TotalCostController", description = "成本汇总")
public class TotalCostController {
    @Autowired
    TotalCostService totalCostService;

    /**
     * 运营费成本汇总Echars图
     *
     * @param summaryOperatingExpensesTO
     * @return
     */
    @ApiOperation(value = "运营费成本汇总Echars图接口", notes = "运营费成本汇总Echars图")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @RequestMapping(value = "getOperateChars", method = RequestMethod.POST)
    public AjaxResult getOperateChars(@RequestBody @Validated SummaryOperatingExpensesTO summaryOperatingExpensesTO) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            List<TlCostOperate> tlCostOperateList= totalCostService.getTlCostOperateList(summaryOperatingExpensesTO, formatter);
            return AjaxResult.success(totalCostService.getOperateChars(formatter,tlCostOperateList,summaryOperatingExpensesTO));
        } catch (ParseException e) {
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }

    /**
     * 运营费成本汇总列表
     *
     * @param summaryOperatingExpensesTO
     * @return
     */
    @ApiOperation(value = "运营费成本汇总列表接口", notes = "运营费成本汇总列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @RequestMapping(value = "getopeList", method = RequestMethod.POST)
    public AjaxResult<Object> getopeList(@RequestBody @Validated SummaryOperatingExpensesTO summaryOperatingExpensesTO) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月");
            List<TlCostOperate> tlCostOperateList= totalCostService.getTlCostOperateList(summaryOperatingExpensesTO, formatter);
            return AjaxResult.success(
                    Tools.buildTableDataInfo(summaryOperatingExpensesTO.getPage(), summaryOperatingExpensesTO.getSize()
                            , totalCostService.getopeList(formatter,tlCostOperateList,summaryOperatingExpensesTO)));
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("加载失败！");
        }
    }

    /**
     * 人员成本汇总列表
     *
     * @param summaryPersonnelCostsTO
     * @return
     */
    @ApiOperation(value = "人员费成本汇总图接口", notes = "人员费成本汇总图")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @RequestMapping(value = "getempChars", method = RequestMethod.POST)
    public AjaxResult<Object> getempChars(@RequestBody @Validated SummaryPersonnelCostsTO summaryPersonnelCostsTO) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            List<TlEmpSalary> empSalaryList = totalCostService.getTlEmpSalaryList(summaryPersonnelCostsTO);
            List<TlEmpIncidental> empIncidentalList = totalCostService.getTlEmpIncidentalList(summaryPersonnelCostsTO);
            return AjaxResult.success(totalCostService.getempChars(formatter,empSalaryList,empIncidentalList,summaryPersonnelCostsTO));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("加载失败！");
        }
    }

    /**
     * 人员成本汇总列表
     *
     * @param summaryPersonnelCostsTO
     * @return
     */
    @ApiOperation(value = "人員费成本汇总列表接口", notes = "人员费成本汇总列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @RequestMapping(value = "getempList", method = RequestMethod.POST)
    public AjaxResult<Object> getempList(@RequestBody @Validated SummaryPersonnelCostsTO summaryPersonnelCostsTO) {
        try {
            List<TlEmpSalary> empSalaryList = totalCostService.getTlEmpSalaryList(summaryPersonnelCostsTO);
            List<TlEmpIncidental> empIncidentalList = totalCostService.getTlEmpIncidentalList(summaryPersonnelCostsTO);
            return AjaxResult.success(Tools.buildTableDataInfo(summaryPersonnelCostsTO.getPage(),
                    summaryPersonnelCostsTO.getSize(), totalCostService.getempList(empSalaryList,empIncidentalList,summaryPersonnelCostsTO)));
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("加载失败！");
        }
    }

    /**
     * 车辆成本汇总图
     *
     * @param summaryVehicleCostTO
     * @return
     */
    @ApiOperation(value = "车辆费成本汇总图接口", notes = "车辆费成本汇总图")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @RequestMapping(value = "getcarChars", method = RequestMethod.POST)
    public AjaxResult<Object> getcarChars(@RequestBody @Validated SummaryVehicleCostTO summaryVehicleCostTO) {
        try {
            SummaryVehicleCostDTO summaryVehicleCostDTO =totalCostService.buildSummaryVehicleCostDTO(summaryVehicleCostTO);
            return AjaxResult.success(totalCostService.getcarChars(summaryVehicleCostDTO,summaryVehicleCostTO));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("加载失败！");
        }
    }

    /**
     * 车辆成本汇总列表
     *
     * @param summaryVehicleCostTO
     * @return
     */
    @ApiOperation(value = "车辆费成本汇总列表接口", notes = "车辆费成本汇总列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @RequestMapping(value = "getcarList", method = RequestMethod.POST)
    public AjaxResult<Object> getcarList(@RequestBody @Validated SummaryVehicleCostTO summaryVehicleCostTO) {
        try {
            SummaryVehicleCostDTO summaryVehicleCostDTO =totalCostService.buildSummaryVehicleCostDTO(summaryVehicleCostTO);
            return AjaxResult.success(Tools.buildTableDataInfo(summaryVehicleCostTO.getPage(),summaryVehicleCostTO.getSize(),totalCostService.getcarList(
                    summaryVehicleCostDTO,summaryVehicleCostTO)));
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("加载失败！");
        }
    }

    /**
     * 总成本汇总图
     *
     * @param summaryTotalCostTO
     * @return
     */
    @ApiOperation(value = "总成本汇总图接口", notes = "总成本汇总图")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @RequestMapping(value = "gettotalChars", method = RequestMethod.POST)
    public AjaxResult<Object> gettotalChars(@RequestBody @Validated SummaryTotalCostTO summaryTotalCostTO) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            SummaryOperatingExpensesTO summaryOperatingExpensesTO = JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO),SummaryOperatingExpensesTO.class);
            List<TlCostOperate> costOperateList = totalCostService.getTlCostOperateList(summaryOperatingExpensesTO, formatter);
            SummaryPersonnelCostsTO summaryPersonnelCostsTO=JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO),SummaryPersonnelCostsTO.class);
            List<TlEmpSalary> empSalaryList = totalCostService.getTlEmpSalaryList(summaryPersonnelCostsTO);
            List<TlEmpIncidental> empIncidentalList = totalCostService.getTlEmpIncidentalList(summaryPersonnelCostsTO);
            SummaryVehicleCostTO summaryVehicleCostTO = JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO),SummaryVehicleCostTO.class);
            SummaryVehicleCostDTO summaryVehicleCostDTO = totalCostService.buildSummaryVehicleCostDTO(summaryVehicleCostTO);
            return AjaxResult.success(totalCostService.gettotalChars(formatter,costOperateList,empSalaryList,empIncidentalList,summaryVehicleCostDTO,summaryTotalCostTO));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("加载失败！");
        }
    }

    /**
     * 总成本汇总列表
     *
     * @param summaryTotalCostTO
     * @return
     */
    @ApiOperation(value = "总成本汇总列表接口", notes = "总成本汇总列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @RequestMapping(value = "gettotalList", method = RequestMethod.POST)
    public AjaxResult<Object> gettotalList(@RequestBody @Validated SummaryTotalCostTO summaryTotalCostTO) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            SummaryOperatingExpensesTO summaryOperatingExpensesTO = JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO),SummaryOperatingExpensesTO.class);
            List<TlCostOperate> costOperateList = totalCostService.getTlCostOperateList(summaryOperatingExpensesTO, formatter);
            SummaryPersonnelCostsTO summaryPersonnelCostsTO=JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO),SummaryPersonnelCostsTO.class);
            List<TlEmpSalary> empSalaryList = totalCostService.getTlEmpSalaryList(summaryPersonnelCostsTO);
            List<TlEmpIncidental> empIncidentalList = totalCostService.getTlEmpIncidentalList(summaryPersonnelCostsTO);
            SummaryVehicleCostTO summaryVehicleCostTO = JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO),SummaryVehicleCostTO.class);
            SummaryVehicleCostDTO summaryVehicleCostDTO = totalCostService.buildSummaryVehicleCostDTO(summaryVehicleCostTO);
            return AjaxResult.success(Tools.buildTableDataInfo(summaryTotalCostTO.getPage(),summaryTotalCostTO.getSize(),totalCostService.gettotalList(formatter,costOperateList,empSalaryList,empIncidentalList,summaryVehicleCostDTO,summaryTotalCostTO)));
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("加载失败！");
        }
    }
}

